<template>
  <div class="addDoctor">
    <div class="container">
      <Form ref="formValidate" :model="formValidate" :label-width="40" label-position="left" :rules="ruleValidate">
        <Row>
          <Col span="24" class="left-part">
            <h2 class="title">基本信息</h2>
            <Row style="margin: 0 15px" :gutter="32">
              <Col span="24" class="upload-wrapper baseInfo-lineOne" style="margin-bottom:20px">
                <label >头像：</label>
                <div class="upload-part" style="margin-top:20px;width:120px">
                  <div
                    class="demo-upload-list"
                    v-for="(item,index) in uploadListAvatar"
                    :key="index"
                  >
                    <template v-if="mode !== 'review'">
                      <div v-if="item.status === 'finished'">
                        <img :src="fileBaseUrl + item.res.fileName" />
                        <div class="demo-upload-list-cover">
                          <Icon type="ios-eye-outline" @click.native="handleView(item)"></Icon>
                          <Icon type="ios-trash-outline" @click.native="onremove('Avatar', item)"></Icon>
                        </div>
                      </div>
                      <div v-else>
                        <Progress :percent="item.percentage" hide-info></Progress>
                      </div>
                    </template>
                    <template v-else>
                      <div>
                        <img :src="fileBaseUrl + item.res.fileName" />
                        <div class="demo-upload-list-cover">
                          <Icon type="ios-eye-outline" @click.native="handleView(item)"></Icon>
                        </div>
                      </div>
                    </template>
                  </div>
                  <Upload
                    v-show="uploadListAvatar.length ==0"
                    ref="uploadAvatar"
                    :headers="fromData"
                    :action="uploadUrl"
                    :default-file-list="defaultListAvatar"
                    :format="['jpg','jpeg','png']"
                    :on-format-error="uploadFormatErrorAvatar"
                    max-size="200"
                    :on-exceeded-size="handleMaxSize"
                    :before-upload="() => preview('Avatar')"
                    :data="uploadDataAvatar"
                    :on-success="(res, file) => handleSuccess(res, file, 'Avatar')"
                    :show-upload-list="noshow"
                    multiple
                    type="drag"
                  >
                    <!-- <Button v-if="mode !== 'review'" type="primary" class="button-upload">选择头像上传</Button> -->
                    <div v-if="mode !== 'review'" style="width: 98px;height:98px;line-height: 110px;background: #f6f6f6;">
                        <Icon type="ios-camera" size="40"></Icon>
                    </div>
                  </Upload>
                </div>
              </Col>
              <Col span="10">
                <!-- 所属医院 -->
                <FormItem label="所属机构：" class="mock-require hongdian" :label-width="80"  v-if="shenfen === 'yaoqi'">
                  <div>
                    {{hospitalEntity.orgName}}
                  </div>
                </FormItem>
                <FormItem label="所属机构：" class="mock-require hongdian" :label-width="80" v-else>                 
                  <div>
                    <!--         :hospitalType="4" -->
                    <fourLevelLinkage
                      v-if="mode !== 'review' && identity!='5'"
                      @changeProvince="changeProvince"
                      @changeCity="changeCity"
                      @changeArea="changeArea"
                      @changeHospital="changeHospital"
                      :province="Number(province)"
                      :city="Number(city)"
                      :area="Number(area)"
                      :hospital="formValidate.hospitalId"
              
                      ref="fourLevelLinkage"
                      style="100%"
                    ></fourLevelLinkage>
                    <div v-if="mode == 'review' && identity!='5'" class="content">
                      <span>{{($store.getters.getProvinceList.filter(item => item.id == province)[0] || {}).name}}</span>
                      <span>{{($store.getters.getCityList(province).filter(item => item.id == city)[0] || {}).name}}</span>
                      <span>{{($store.getters.getAreaList(city).filter(item => item.id == area)[0] || {}).name}}</span>
                      <span>{{hospitalName}}</span>
                    </div>
                    <div class="content"  v-if="identity=='5' ">
                      <span>{{provinceName}}</span>
                      <span>{{cityName}}</span>
                      <span>{{areaName}}</span>
                      <span>{{hospitalName}}</span>
                  </div>
                  </div>
                </FormItem>
                <!-- 药师姓名 -->
                <FormItem label="姓名：" prop="name" :label-width="50" class="hongdian">
                  <Input v-if="mode !== 'review'" v-model="formValidate.name" placeholder="请输入药师姓名" style="width:100%" maxlength="15"></Input>
                  <div v-else class="content">{{formValidate.name}}</div>
                </FormItem>
                <!-- 联系方式 -->
                <FormItem label="手机号：" prop="phone" :label-width="60" class="hongdian">
                  <Input v-if="mode !== 'review'" v-model="formValidate.phone" placeholder="请输入药师联系方式" maxlength="11"></Input>
                  <div v-else class="content">{{formValidate.phone}}</div>
                </FormItem>
              </Col>
              <Col span="7">
                <FormItem label="职称：" prop="titleType" :label-width="50" class="hongdian">
                  <Select v-if="mode !== 'review'" v-model="formValidate.title" placeholder="请选择药师职称">
                    <Option
                      v-for="item in titleList"
                      :value="item.dictType"
                      :key="item.dictType"
                      style="text-align:center"
                    >{{item.dictName}}</Option>
                  </Select>
                  <!-- <div v-else class="content">{{(titleList.filter((item) => formValidate.titleName === item.dictType)[0] || {}).dictName}}</div> -->
                  <div v-else class="content">{{formValidate.titleName}}</div>
                </FormItem>
                <!-- 身份证号 -->
                <FormItem label="身份证号：" prop="idcard" :label-width="75" class="hongdian">
                  <Input v-if="mode !== 'review'" v-model="formValidate.idcard" placeholder="请输入药师身份证号" maxlength="18"></Input>
                  <div v-else class="content">{{formValidate.idcard}}</div>
                </FormItem>
                 <!-- 民族： -->
                <FormItem label="民族：" prop="民族" :label-width="40">
                  <Select v-if="mode !== 'review'" v-model="formValidate.nation" placeholder="请选择民族">
                    <Option
                      v-for="item in nationalityList"
                      :value="item.dictValue"
                      :key="item.dictName"
                      style="text-align:center"
                    >{{ item.dictName }}</Option>
                  </Select>
                  <div v-else class="content">{{(nationalityList.filter((item) => formValidate.nation === item.dictValue)[0] || {}).dictName}}</div>
                </FormItem>
              </Col>
              <Col span="7">
                <!-- 是否驻店 -->             
                <FormItem label="是否驻店：" :label-width="60">
                  <RadioGroup v-if="mode !== 'review'" v-model="formValidate.havStore">
                      <Radio value="0" label="0">否</Radio>
                      <Radio value="1" label="1">是</Radio>
                    </RadioGroup>
                    <div v-else class="content">{{['否', '是'][formValidate.havStore]}}</div>
                </FormItem>
                <!-- 性别 -->
                <FormItem label="性别：" prop="gender" :label-width="50" class="hongdian">
                  <RadioGroup v-if="mode !== 'review'" v-model="formValidate.gender">
                    <Radio :value="1" :label="1">男</Radio>
                    <Radio :value="2" :label="2">女</Radio>
                  </RadioGroup>
                  <div v-else class="content">{{['', '男', '女'][formValidate.gender]}}</div>
                </FormItem>
                <!-- 籍贯 -->
                <FormItem label="籍贯：">
                  <Input v-if="mode !== 'review'" v-model="formValidate.nativePlace" placeholder="请输入籍贯" maxlength="100"></Input>
                  <div v-else class="content">{{formValidate.nativePlace}}</div>
                </FormItem>
              </Col>
              <Col span="17">
                <!-- 住址 -->
                <FormItem label="详细住址：" :label-width="60">
                  <Input v-if="mode !== 'review'" v-model="formValidate.address" placeholder="请输入住址" maxlength="100"></Input>
                  <div v-else class="content">{{formValidate.address}}</div>
                </FormItem>
              </Col>
              <Col span="10">
                <!-- 最高学历 -->
                <FormItem label="最高学历：" :label-width="60">
                  <Select v-if="mode !== 'review'" v-model="formValidate.highestEducation" placeholder="请选择最高学历">
                    <Option
                      v-for="item in educationList"
                      :value="item.dictValue"
                      :key="item.dictValue"
                      style="text-align:center"
                    >{{ item.dictName }}</Option>
                  </Select>
                  <div v-else class="content">{{(educationList.filter((item) => formValidate.highestEducation === item.dictValue)[0] || {}).dictName}}</div>
                </FormItem>
                <FormItem label="专业：">
                    <Select v-if="mode !== 'review'" v-model="formValidate.major" placeholder="请选择专业">
                      <Option
                        v-for="item in majorList"
                        :value="item.dictValue"
                        :key="item.dictValue"
                        style="text-align:center"
                      >{{ item.dictName }}</Option>
                    </Select>
                    <div v-else class="content">{{formValidate.major}}</div>
                  </FormItem>
              </Col>
              <Col span="7">
                <!-- 手机号 -->
                <FormItem label="毕业时间：" :label-width="60">
                  <DatePicker
                    v-if="mode !== 'review'"
                    type="date"
                    v-model="formValidate.graduationTime"
                    placeholder="请选择毕业时间"
                  ></DatePicker>
                  <div v-else class="content">{{formValidate.graduationTime}}</div>
                </FormItem>
                <!-- 学位： -->
                <FormItem label="学位：">
                  <Select v-if="mode !== 'review'" v-model="formValidate.degree" placeholder="请选择学位">
                    <Option
                      v-for="item in degreeList"
                      :value="item.dictValue"
                      :key="item.dictValue"
                      style="text-align:center"
                    >{{ item.dictName }}</Option>
                  </Select>
                  <div v-else class="content">{{(degreeList.filter((item) => formValidate.degree === item.dictValue)[0] || {}).dictName}}</div>
                </FormItem>
              </Col>
              <Col span="7">
                <!-- 联系方式 -->
                <FormItem label="毕业学校：" :label-width="60">
                  <Input v-if="mode !== 'review'" v-model="formValidate.graduateSchool" placeholder="请输入毕业学校" maxlength="100"></Input>
                  <div v-else class="content">{{formValidate.graduateSchool}}</div>
                </FormItem>
                <!-- 毕业证号： -->
                <FormItem label="毕业证号：" :label-width="60">
                  <Input v-if="mode !== 'review'" v-model="formValidate.graduationCard" placeholder="请输入毕业证号" maxlength="30"></Input>
                  <div v-else class="content">{{formValidate.graduationCard}}</div>
                </FormItem>
              </Col>
            </Row>
          </Col>
          <Col span="24" class="right-part">
            <h2 class="title">
              资质信息           
              <span class="tishi" style="margin: 0 15px">(注意：图片只能上传jpg格式，且文件大小不超过200KB)</span>  
            </h2>            
            <Row style="margin: 0 15px" :gutter="32">
              <Col span="8">
                <div class="type1">
                  <div style="font-weight:550;margin:10px 0;">
                    <span style="color:red">*</span>
                    药师资格证书：
                  </div>
                  <Row style="margin-left:25px">
                    <Col span="24">
                      <!-- 证书编号 -->
                      <FormItem
                        label="证书编号："
                        :prop="'certList.' + getItemIndexByType('1') + '.code'"
                        :rules="{required: true, message: '请填写证书编号', trigger: 'blur'}"
                         :label-width="75"
                        class="hongdian"
                      >
                        <Input v-if="mode !== 'review'" v-model="getItemByType('1').code" placeholder="请输入证书编号" maxlength="50"></Input>
                        <div v-else class="content">{{getItemByType('1').code}}</div>
                      </FormItem>
                    </Col>
                    <Col span="24">
                      <!-- 发证日期 -->
                      <FormItem label="批准日期：" :label-width="60">
                        <DatePicker
                          v-if="mode !== 'review'"
                          style="width: 100%;"
                          type="date"
                          v-model="getItemByType('1').details2"
                          placeholder="请选择发证日期"
                        ></DatePicker>
                        <div v-else class="content">{{getItemByType('1').details2?getItemByType('1').details2.substring(0,10):''}}</div>
                      </FormItem>
                    </Col>
                    <Col span="24">
                      <!-- 发证日期 -->
                      <FormItem label="发证日期：" :label-width="60">
                        <DatePicker
                          v-if="mode !== 'review'"
                          style="width: 100%;"
                          type="date"
                          v-model="getItemByType('1').details3"
                          placeholder="请选择签发日期"
                        ></DatePicker>
                        <div v-else class="content">{{getItemByType('1').details3?getItemByType('1').details3.substring(0,10):''}}</div>
                      </FormItem>
                    </Col>
                    <!-- 专业 -->
                    <Col span="24">
                      <FormItem 
                      label="专业："
                      :prop="'certList.' + getItemIndexByType('1') + '.details1'"
                      :rules="{required: true, message: '请选择专业', trigger: 'change'}" :label-width="50" class="hongdian">
                        <Select v-if="mode !== 'review'" v-model="getItemByType('1').details1" placeholder="请选择专业">
                          <Option
                            v-for="item in majorList"
                            :value="item.dictValue"
                            :key="item.dictValue"
                            style="text-align:center"
                          >{{ item.dictName }}</Option>
                        </Select>
                        <div v-else class="content">{{getItemByType('1').details1}}</div>
                      </FormItem>
                    </Col>                     
                    <div class="upload-wrapper">
                      <!-- <label style="width: 85px">证书图片：</label> -->
                      <imgUploadAndReview ref="upload1" v-model="uploadList1"   :mode="mode" :maxLength="-1" :uploadData="uploadData"/>
                    </div>
                  </Row>
                </div>
              </Col>
              <Col span="8">
                <div class="type2">
                  <div style="font-weight:550;margin:10px 0;">
                    <span style="color:red">*</span>
                    药师执业证书：
                  </div>
                  <Row style="margin-left:25px">
                    <Col span="24">
                      <!-- 证书编号 -->
                      <FormItem 
                        label="注册证编号：" 
                        :prop="'certList.' + getItemIndexByType('2') + '.code'"
                        :rules="{required: true, message: '请填写执业证书编号', trigger: 'blur'}" :label-width="90" class="hongdian">
                        <Input v-if="mode !== 'review'" v-model="getItemByType('2').code" placeholder="请输入证书编号" maxlength="50"></Input>
                        <div v-else class="content">{{getItemByType('2').code}}</div>
                      </FormItem>
                    </Col>
                    <Col span="24">
                      <!-- 证书编号 -->
                      <FormItem 
                        label="资格证书号：" 
                        :prop="'certList.' + getItemIndexByType('2') + '.details1'"
                        :rules="{required: true, message: '请填写资格证书编号', trigger: 'blur'}" :label-width="90" class="hongdian">
                        <Input v-if="mode !== 'review'" v-model="getItemByType('2').details1" placeholder="请输入证书编号" maxlength="50"></Input>
                        <div v-else class="content">{{getItemByType('2').details1}}</div>
                      </FormItem>
                    </Col>
                    <Col span="24">
                      <!-- 发证日期 -->
                      <FormItem label="发证日期：" :label-width="60">
                        <DatePicker
                          v-if="mode !== 'review'"
                          style="width: 100%;"
                          type="date"
                          v-model="getItemByType('2').details2"
                          placeholder="请选择发证日期"
                        ></DatePicker>
                        <div v-else class="content">{{getItemByType('2').details2?getItemByType('2').details2.substring(0,10):''}}</div>
                      </FormItem>
                    </Col>
                    <Col span="24">
                      <!-- 发证日期 -->
                      <FormItem label="有效期至：" :label-width="60">
                        <DatePicker
                          v-if="mode !== 'review'"
                          style="width: 100%;"
                          type="date"
                          v-model="getItemByType('2').details3"
                          placeholder="请选择有效期"
                        ></DatePicker>
                        <div v-else class="content">{{getItemByType('2').details3?getItemByType('2').details3.substring(0,10):''}}</div>
                      </FormItem>
                    </Col>
                    <div class="upload-wrapper">
                      <!-- <label style="width: 85px">证书图片：</label> -->
                      <imgUploadAndReview ref="upload2" v-model="uploadList2"  :mode="mode" :maxLength="-1" :uploadData="uploadData"/>
                    </div>
                  </Row>
                </div>
              </Col>
              <Col span="8">
                <div class="type3">
                  <div style="font-weight:550;margin:10px 0;">
                    <!-- <span style="color:red">*</span> -->
                    其他证书：
                  </div>
                  <Col style="margin-left:15px">
                      <Input
                        v-if="mode !== 'review'"
                        v-model="getItemByType('3').code"
                        type="textarea"
                        :autosize="{minRows: 2,maxRows: 5}"
                        placeholder="请输入其他证书说明"
                        maxlength="50"
                      ></Input>
                      <div v-else class="content">{{getItemByType('3').code}}</div>
                    <!-- </FormItem> -->
                  </Col>
                  <div class="upload-wrapper" style="margin-top:30px;margin-left:15px">
                    <!-- <label style="width: 85px">证书图片：</label> -->
                    <imgUploadAndReview ref="upload3" v-model="uploadList3"  :mode="mode" :maxLength="-1" :uploadData="uploadData"/>
                  </div>
                </div>
              </Col>
            </Row>
          </Col>
          <Col span="24" class="left-part">
            <h2 class="title">药师简介</h2>
            <Row style="margin: 0 15px">
              <Col span="24">
                <!-- 医生擅长 -->
                <div style="font-weight:550;margin-bottom:10px;">
                  药师擅长：
                </div>
                <FormItem style="margin-bottom:15px">
                  <Input
                    v-if="mode !== 'review'"
                    v-model="formValidate.doctorGood"
                    type="textarea"
                    :autosize="{minRows: 2,maxRows: 5}"
                    placeholder="请输入药师擅长信息，便于平台推荐。"
                    maxlength="500" show-word-limit
                  ></Input>
                  <div v-else class="content">{{formValidate.doctorGood}}</div>
                </FormItem>
              </Col>
              <Col span="24">
                <!-- 药师简介 -->
                <div style="font-weight:550;margin-bottom:10px;">
                  个人简介：
                </div>
                <FormItem style="margin-bottom:15px">
                  <Input
                    v-if="mode !== 'review'"
                    v-model="formValidate.personalIntroduction"
                    type="textarea" maxlength="1000"
                    :autosize="{minRows: 2,maxRows: 5}"
                    placeholder="请输入药师个人简介"
                  ></Input>
                  <div v-else class="content">{{formValidate.personalIntroduction}}</div>
                </FormItem>
              </Col>
            </Row>
          </Col>
        </Row>
      </Form>
      <!-- 添加药师 -->
      <div class="btn-group">
        <Button type="primary" v-if="mode === 'add'" @click="handleConfirm('add')" class="btn">
          <Icon type="ios-checkmark" style="margin-right: 5px" />提交
        </Button>
        <Button type="primary" v-if="mode === 'edit'" @click="handleConfirm('edit')" class="btn">
          <Icon type="ios-checkmark" style="margin-right: 5px" />保存修改
        </Button>
        <Button style="marginLeft: 20px" @click="back"  class="btn">
          <i class = "iconfont icon-butongyi" style="font-size:14px;margin-right:5px"></i>取消
        </Button>
      </div>
    </div>
    <Modal title="预览图片" width="30" v-model="viewModal" footer-hide>
      <div>
        <img :src="viewSrc" style="width: 100%" />
      </div>
    </Modal>
    <Spin v-if="showSpin" fix></Spin>
  </div>
</template>
<script>
import api from "@/api/commonApi";
import code from "@/configs/base.js";
import Utils from "@/utils";
import cookie from "@/utils/cookie.js";
import fourLevelLinkage from "@/components/fourLevelLinkage";
import imgUploadAndReview from "@/components/imgUploadAndReview";
import { DatePicker } from "view-design";
export default {
  components: {
    DatePicker,
    fourLevelLinkage,
    imgUploadAndReview
  },
  props: {
    mode: String
  },
  data() {
    return {
      //权限相关
      shenfen: '',
      hospitalEntity: {},

      showSpin: false,
      noshow: false,
      province: null,
      city: null,
      area: null,
      hospital: null,
      provinceName: null,
      cityName: null,
      areaName: null,
      detfaltList: [],
      btnLoading: false,
      formValidate: {
        // 头像
        docIcon: "",
        // 姓名
        name: null,
        // 性别
        gender: null,
        // 联系方式
        phone: null,
        // 民族
        nation: null,
        // 身份证号
        idcard: null,
        // 从业年限
        // workingYears: null,
        // 关联医院
        hospitalId: null,
        // 住址
        address: null,
        // 籍贯
        nativePlace: null,
        // 最高学历
        highestEducation: null,
        // 毕业时间
        graduationTime: null,
        // 毕业学校
        graduateSchool: null,
        // 专业
        major: null,
        // 毕业证号
        graduationCard: null,
        // 学位
        degree: null,
        // 个人简介
        personalIntroduction: null,
        // 擅长疾病
        doctorGood: null,
        // 资质信息
        certList: [
          {
            code: "", //证书编号
            details1: null, // 专业
            details2: null, // 批准日期
            details3: null, // 签发日期
            filePath: null,
            type: "1"
          },
          {
            code: null, //证书编号
            details1: null, // 资格证书号
            details2: null, // 发证日期
            details3: null, // 有效期至
            filePath: null,
            type: "2"
          },
          {
            code: null, // 证书说明
            filePath: null,
            type: "3"
          }
        ],

        // 职称
        titleType: null,
        // 科室
        // deptType: null,
        // 是否驻点
        havStore: null,
        type: 2,
      },
      ruleValidate: {
        name: [{ required: true, message: "请输入药师姓名", trigger: "change" }],
        gender: [{ required: true, type: 'number', message: "请选择药师性别", trigger: "change" }],
        phone: [{ required: true, message: "请输入正确的联系方式", trigger: "change", pattern: Utils.pattern.phone}],
        idcard: [
          { required: true, message: "请输入正确的身份证号", trigger: "change", pattern: Utils.pattern.idCard }
        ],
        deptType: [{ required: true, message: "请选择科室", trigger: "change" }],
        hiscode: [{ required: true, message: "请输入HIS编码", trigger: "change" }],
        title: [{ required: true, message: "请选择药师职称", trigger: "change" }],
      },
      wyAccid: '', // 云信id;
      // 上传图片相关
      uploadData: { json: '{"urlCode":"' + code.urlCode.doctor + '"}' },
      uploadDataAvatar: { json: '{"urlCode":"' + code.urlCode.reviewAvatar + '"}' },
      /**
       * 下拉选项
       * **/
      departmentList: [],// 科室列表
      titleList: [], // 职称列表
      majorList: [], // 专业列表
      nationalityList: [], // 民族列表
      educationList: [], // 学历列表
      degreeList: [], // 学位列表
      uploadListAvatar: [],
      uploadList1: [],
      uploadList2: [],
      uploadList3: [],
      uploadUrl: api.fileAll,
      jgtype: '',
      hospitalId: '',
      viewModal: false,
      identity:'',
    };
  },
  created() {
    this.hospitalId = this.$route.query.hospitalId ? this.$route.query.hospitalId : null;
    this.jgtype = this.$route.query.jgtype
    let breadList = [
      { path: "/index", title: "首页" },
      {
        path: "/index/maintain/indexManagement/index",
        title: "索引管理"
      },
      {
        path: "/index/doctorOperation/pharmacyregister/list",
        title: "药师注册信息"
      }
    ];
    this.$emit("changeBreadList", breadList);
    // 权限信息初始化
    this.authConfig();
  },
  mounted() {
    this.identity = cookie.getCookie("idtt");
    if(this.identity ==5){
      this.provinceName = cookie.getCookie("provinceName");
      this.cityName = cookie.getCookie("cityName");
      this.areaName = cookie.getCookie("districtName");
      this.hospitalName = cookie.getCookie("orgName");
    }
    this.uploadListAvatar = this.$refs.uploadAvatar.fileList;
    // 加载职称列表
    this.getTitleList();
    this.findDoctorDict();
    if (this.mode !== 'add') {
      this.findKbaoOperateDoctorDtoById();
    }
  },
  methods: {
    authConfig() {
        // 如果是药企账号登录所属药企选项默认写死当前账号。 
        const userIsYq = cookie.getCookie("userIsYq");
        if (userIsYq === 'true') {
          const hospitalEntity = cookie.getCookie("hospitalEntity");
          this.shenfen = 'yaoqi';
          this.hospitalEntity = JSON.parse(hospitalEntity);
          this.formValidate.hospitalId = this.hospitalEntity.id;
        }
    },
    changeProvince(val) {
      this.province = val;
    },
    changeCity(val) {
      this.city = val;
    },
    changeArea(val) {
      this.area = val;
    },
    changeHospital(val) {
      if (!val && val !== 0) {
        this.formValidate.hospitalId = null;
        // this.hospital = null;
        this.departmentList = [];
        return;
      }   
      this.formValidate.hospitalId = val;
      this.hospital = val;
      if (Boolean(val)) {
        this.getDepartmentList(val);
      }
    },
    back() {
      let pageNo = this.$route.query.pageNo;
      if(this.jgtype == 'jianguan'){
        this.$router.back(-1);
        // this.functionJS.queryNavgationTo(this, "/index/supervision/qualificationApply/add", {
        // });
      } else {
        this.functionJS.queryNavgationTo(this, "/index/doctorOperation/pharmacyregister/list", {
          pageNo
        });
      } 
    },
    // 获取职称列表
    getTitleList() {
      this.$axios
        .post(api.findDoctorTitleListByDoctorType,{type: 2})
        .then(resp => {
          console.log(resp);
          this.titleList = resp.data.object;
        })
        .catch(err => {
          console.log(err);
        });
    },
    findDoctorDict() {
      this.$axios.post(api.findDoctorDict,{}).then(resp => {
        if (resp.data.code == 1) {
          const resObj = resp.data.object;
          this.majorList = resObj.pharmacistmajorList;
          this.nationalityList = resObj.nationalityList;
          this.educationList = resObj.educationList;
          this.degreeList = resObj.degreeList;
        } else {
          this.$Message.error(resp.data.message);
        }
      }).catch(err => {
          console.log(err);
      });
    },
    // 通过选择医院加载医院下科室
    getDepartmentList(hospitalId) {
      let url = api.querydeptname;
      let params = {
        hospitalId
      };
      this.packageAxios(this, url, params, res => {
        if (res.data.success) {
          let ret = res.data.object;
          this.departmentList = ret;
          console.log(ret);
        } else {
          this.$Message.error("加载失败");
        }
      });
    },
    // 文件上传成功触发的函数
    handleSuccess(res, file, type) {
      res = this.uploadFileDecrypt(res);

      if (res.success) {
        file.res = res.object[0];
      } else {
        this.$Message.info("上传失败,请重试");
      }
    },
     preview(type) {
      if (type === "Avatar") {
        const check = this["uploadList" + type].length < 1;
        if (!check) {
          this.$Message.info("只能上传一张图片");
          return false;
        }
      }
    },
    onremove(type, file) {
      const fileList = this.$refs["upload" + type].fileList;
      this.$refs["upload" + type].fileList.splice(fileList.indexOf(file), 1);
    },
    uploadFormatErrorAvatar() {
      this.$Message.error("上传图片类型只能是jpg/jpeg/png格式");
    },
    getItemByType(type) {
      return this.formValidate.certList.filter(item => item.type == type)[0];
    },
    getItemIndexByType(type) {
      var resIndex;

      this.formValidate.certList.forEach((item, index) => {
        if (item.type == type) {
          resIndex = index
        }
      });
      return resIndex; 
    },
    // 上传图片大小错误提醒
    handleMaxSize() {
      this.$Message.error("上传图片大小不能超过200kb");
    },
    // 查看预览图片
    handleView(item) {
      this.viewModal = true;
      this.viewSrc = this.fileBaseUrl + item.res.fileName;
    },
    findKbaoOperateDoctorDtoById() {
      let url;
      if (this.jgtype == 'jianguan') {
        url = api.findOperateSuperviseDoctorById
      } else if (this.jgtype != 'jianguan') {
        url = api.findKbaoOperateDoctorDtoById;
      }
      this.showSpin = true;
      this.$axios.post(url, {id: this.$route.query.id}).then(res => {
        if (res.data.code === 1) {
           // 利用返回数据填充 前端数据
          const resObj = JSON.parse(JSON.stringify(res.data.object));
          const ret = res.data.object;
          if(resObj.certList){
            //对 resObj.certlist 进行 this.formValidate.certListmerge 进行merge 以兼容旧数据;
            this.formValidate.certList.forEach((item, index) => {
              resObj.certList[index] = Object.assign({}, item, resObj.certList[index] || {})
            });
            resObj.certList.forEach((item, index) => {
              if (item.filePath) {
                item.filePath = JSON.parse(item.filePath);
                  if (!Array.isArray(item.filePath)) {
                    item.filePath = [item.filePath];
                  }
                  item.filePath.forEach((item2, index) => {
                    this['uploadList' + item.type].push({
                      status: "finished",
                      url: item2.fileName,
                      res: item2,
                      uid: index
                    });
                  });
              }
            });
          }
          // 头像
          try {
            if (resObj.docIcon) {
              const iconInfo = JSON.parse(resObj.docIcon);
              this.$refs.uploadAvatar.fileList.push({
                  status: "finished",
                  url: iconInfo.fileName,
                  res: iconInfo,
                  uid: 0
                });
            }
          } catch (err) {
            this.$Message.error("头像解析错误");
          }
          // 预留返回的有字段数据
          this.province = String(resObj.province);
          this.city = String(resObj.cityCode);
          this.area = String(resObj.areaCode);
          this.hospital = String(resObj.hospitalId);
          this.hospitalName = String(resObj.hospitalName);
          // 字段错误纠正（与提交数据字段不对应等问题）
          resObj.deptType = resObj.deptTypeId;
          this.wyAccid = resObj.wyAccid;
          // 删除多余字段 使其与要提交字段保持一直
          for (let i in resObj) {
            if (!this.formValidate.hasOwnProperty(i)) {
              delete resObj[i]
            }
          }
        
          Object.assign(this.formValidate,JSON.parse(JSON.stringify(resObj)), ret);
          if (this.hospital && this.mode !== 'add') {
            this.getDepartmentList(this.hospital);
          }
        } else {
          this.$Message.error(res.data.message);
        }
        this.showSpin = false;
      });
    },

    handleConfirm(type) {
      this.$refs['formValidate'].validate(valid => {
        if (valid) {
          let url = api.insertdoctor;
          if(this.jgtype == 'jianguan' && type === 'add'){
            this.formValidate.hospitalId = this.hospitalId
            url = api.insertSuperviseDoctor
          } else if (this.jgtype != 'jianguan' && type === 'add') {
            url = api.insertdoctor
          } else if (this.jgtype == 'jianguan' && type === 'edit') {
            url = api.insertSuperviseDoctor
            this.formValidate.hospitalId = this.hospitalId
            this.formValidate.id = this.$route.query.id;
          } else if (this.jgtype != 'jianguan' && type === 'edit') {
            url = api.updateKbadDoctor;
            this.formValidate.id = this.$route.query.id;
          }
          if (!this.formValidate.hospitalId && this.formValidate.hospitalId !== 0) {
            this.$Message.error('请选择所在医院');
            return false;
          }
          // 上传地址字段内容填充
          this.formValidate.certList.forEach((item, index) => {
            if (item.type == '3') {
              item.filePath = this['uploadList' + item.type].map((item) => item.res)
            } else {
              item.filePath = this['uploadList' + item.type].map((item) => item.res);
              item.details2 = Utils.getNowDateBySpecialFormat(item.details2);
              item.details3 = Utils.getNowDateBySpecialFormat(item.details3);
            }
          });
          this.formValidate.docIcon = this.uploadListAvatar.map(item => item.res)[0] ? JSON.stringify(this.uploadListAvatar.map(item => item.res)[0]) : '';
          // 数据类型转换
          this.formValidate.graduationTime = Utils.getNowDateBySpecialFormat(this.formValidate.graduationTime);
          this.formValidate.hospitalid = this.formValidate.hospitalId;
          this.btnLoading = true;
          this.titleList.forEach(i =>{
            if(i.dictType == this.formValidate.title){
                this.formValidate.titleName = i.dictName
            }
          })
          console.log(JSON.stringify(this.formValidate))
          console.log('入参',this.formValidate)
          this.$axios.post(url, this.formValidate).then(res => {
            if (res.data.success) {
              type === 'add' && this.$Message.success("添加成功");
              type === 'edit' && this.$Message.success("保存成功");
              this.back();
            } else {
              this.$Message.error(res.data.message);
            }
            this.btnLoading = false;
          }).catch(() => {
            this.btnLoading = false;
          });
        } else {
          this.$Message.error("请完整填写必填项!");
        }
      });
    },
  },
  watch: {
    province(n) {
      console.log('province', n)
    },
    city(n) {
      console.log('city', n)
    },
    area(n) {
      console.log('area', n)
    },
    'formValidate.hospitalId'(n) {
      console.log('hospitalId', n)
    }
  }
};
</script>
<style lang="less" scoped>
.addDoctor {
  width: 100%;
  background: #ffffff;
  box-sizing: border-box;
  font-size: 14px;
  /deep/ .ivu-form .ivu-form-item-label {
    padding-right: 0px;
  }
  .hongdian{
    margin-left:-10px
  }
  .left-part {
    width: 98%;
    margin: 20px auto;
    margin-left: 1%;
    margin-bottom: 20px;
    border: 1px solid #f0f0f0;
    box-shadow: 0 4px 3px #ebedf8;
    border-radius: 5px;
    .title {
      font-size: 20px;
      padding-left: 15px;
      margin-bottom: 20px;
      color: #333;
      height: 40px;
      line-height: 40px;
      background: #ebedf8;         
    }
  }
  .right-part {
    width: 98%;
    margin-left: 1%;
    border: 1px solid #f0f0f0;
    box-shadow: 0 4px 3px #ebedf8;
    border-radius: 5px;
    padding-bottom: 20px;
    .title {
      font-size: 20px;
      padding-left: 15px;
      margin-bottom: 10px;
      color: #333;
      height: 40px;
      line-height: 40px;
      background: #ebedf8;
    }
    .tishi {
        color: #FF9966;
        font-size: 14px;
        margin-left:35px;
      }
    .box-border {
      border: 1px solid #ccc;
      padding: 10px 5px;
    }
  }
}
.btn-group {
    padding: 20px 0;
    text-align: center;
    .btn {
      width: 85px;
      height: 35px;
    }
  }
.upload-wrapper {
  display: flex;
  align-items: center;
  clear: both;
  .upload-part {
    display: flex;
    position: relative;
    align-items: center;
    .demo-upload-list {
      display: inline-block;
      width: 100px;
      height: 100px;
      text-align: center;
      line-height: 60px;
      border: 1px solid transparent;
      border-radius: 4px;
      overflow: hidden;
      background: #fff;
      position: relative;
      box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
      margin-right: 4px;
    }
    // 关闭input文本框自动填充背景色黄色
    input:-webkit-autofill {
      box-shadow: 0 0 0px 1000px white inset !important;
    }

    .demo-upload-list img {
      width: 100%;
      height: 100%;
    }
    .demo-upload-list-cover {
      display: none;
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      background: rgba(0, 0, 0, 0.6);
    }
    .demo-upload-list:hover .demo-upload-list-cover {
      display: block;
    }
    .demo-upload-list-cover i {
      color: #fff;
      font-size: 20px;
      cursor: pointer;
      margin: 0 2px;
    }
  }
  label {
    text-align: right;
    padding: 10px 0px 10px 0;
  }
}
.mock-require {
  /deep/ label:before {
    content: '*';
    display: inline-block;
    margin-right: 4px;
    line-height: 1;
    font-family: SimSun;
    font-size: 12px;
    color: #ed3f14;
  }
}
</style>

